package com.ui.basic.service.trans;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.basic.util.ComboBoxPopulator;
import com.ui.basic.command.trans.CommandCashtrans;
import com.ui.basic.ibatis.dao.admin.TblACustomerDAO;
import com.ui.basic.ibatis.dao.system.TblSLocDAO;
import com.ui.basic.ibatis.dao.trans.TblTCashtransDAO;
import com.ui.basic.ibatis.model.admin.TblACustomerExample;
import com.ui.basic.ibatis.model.system.TblSLocExample;
import com.ui.basic.ibatis.model.trans.TblTCashtransExample;

@Service("cashTransService")
public class CashTransServiceImpl implements CashTransService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(CashTransServiceImpl.class);

	@Autowired
	private TblTCashtransDAO tblTCashtransDAO;
	@Autowired
	private TblSLocDAO tblSLocDAO;
	@Autowired
	private TblACustomerDAO tblACustomerDAO;
	
	public void setTblTCashtransDAO(TblTCashtransDAO tblTCashtransDAO) {
		this.tblTCashtransDAO = tblTCashtransDAO;
	}

	public void setTblACustomerDAO(TblACustomerDAO tblACustomerDAO) {
		this.tblACustomerDAO = tblACustomerDAO;
	}

	public void setTblSLocDAO(TblSLocDAO tblSLocDAO) {
		this.tblSLocDAO = tblSLocDAO;
	}

	public void setCombo(CommandCashtrans commandCashTrans) throws SQLException {
		LOGGER.debug(">>>>CashTransServiceImpl.setCombo");
		
		TblSLocExample locExample = new TblSLocExample();
		
		locExample.setOrderByClause("LOC_NAME");
		commandCashTrans.setLocList(ComboBoxPopulator.populate(
				tblSLocDAO.selectByExample(locExample), "id","LOC_NAME"));

		TblACustomerExample customerExample = new TblACustomerExample();
		
		//customerExample.setOrderByClause("");
		commandCashTrans.setCustList(ComboBoxPopulator.populate(
				tblACustomerDAO.selectByExample(customerExample), "id","firstname"));
	}

	public CommandCashtrans getCashTrans(CommandCashtrans commandCashTrans)
			throws SQLException {
		LOGGER.debug(">>>>CashTransServiceImpl.getCashTrans");
		if (commandCashTrans != null) {
			TblTCashtransExample example = new TblTCashtransExample();
			if(commandCashTrans.getLocId()!=null && commandCashTrans.getLocId()>0){
				example.createCriteria().andLocIdEqualTo(commandCashTrans.getLocId());
			}

			int rec = 0;
			try {
				rec = tblTCashtransDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}

			commandCashTrans.calculate(rec);
			example.setIntRowNum((commandCashTrans.getPgIndex() - 1)
					* commandCashTrans.getPgRows());
			example.setIntRowsPerPage(commandCashTrans.getPgRows());
			example.setOrderByClause("TRANS_DATE");
			commandCashTrans.setData(tblTCashtransDAO.selectByExample(example));
		}
		return commandCashTrans;
	}

	public CommandCashtrans getCashTransDetail(CommandCashtrans commandCashtrans)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
